home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / html.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  26.2 KB  |  961 lines

  1. /*        Structured stream to Rich hypertext converter
  2. **        ============================================
  3. **
  4. **    This generates of a hypertext object.  It converts from the
  5. **    structured stream interface fro HTMl events into the style-
  6. **    oriented iunterface of the HText.h interface.  This module is
  7. **    only used in clients and shouldnot be linked into servers.
  8. **
  9. **    Override this module if making a new GUI browser.
  10. **
  11. */
  12. #include"capalloc.h"
  13. #include "HTML.h"
  14.  
  15. /* #define CAREFUL         Check nesting here notreally necessary */
  16.  
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include"capstdio.h"
  20.  
  21. #include "HTAtom.h"
  22. #include "HTChunk.h"
  23. #include "HText.h"
  24. #include "HTStyle.h"
  25.  
  26. #include "HTAlert.h"
  27. #include "HTMLGen.h"
  28. #include "HTParse.h"
  29.  
  30. extern HTStyleSheet * styleSheet;    /* Application-wide */
  31.  
  32. /*    Module-wide style cache
  33. */
  34. PRIVATE int         got_styles = 0;
  35. PRIVATE HTStyle *styles[HTML_ELEMENTS];
  36. PRIVATE HTStyle *default_style;
  37.  
  38.  
  39. /*        HTML Object
  40. **        -----------
  41. */
  42. #define MAX_NESTING 20        /* Should be checked by parser */
  43.  
  44. typedef struct _stack_element {
  45.         HTStyle *    style;
  46.     int        tag_number;
  47. } stack_element;
  48.  
  49. struct _HTStructured {
  50.     CONST HTStructuredClass *     isa;
  51.     HTParentAnchor *         node_anchor;
  52.     HText *             text;
  53.  
  54.     HTStream*            target;            /* Output stream */
  55.     HTStreamClass        targetClass;        /* Output routines */
  56.  
  57.     HTChunk             title;        /* Grow by 128 */
  58.     
  59.     char *            comment_start;    /* for literate programming */
  60.     char *            comment_end;
  61.  
  62.     HTTag *            current_tag;
  63.     BOOL            style_change;
  64.     HTStyle *            new_style;
  65.     HTStyle *            old_style;
  66.     BOOL            in_word;  /* Have just had a non-white char */
  67.     stack_element     stack[MAX_NESTING];
  68.     stack_element     *sp;        /* Style stack pointer */
  69. };
  70.  
  71. struct _HTStream {
  72.     CONST HTStreamClass *    isa;
  73.     /* .... */
  74. };
  75.  
  76. /*        Forward declarations of routines
  77. */
  78. PRIVATE void get_styles NOPARAMS;
  79.  
  80.  
  81. PRIVATE void actually_set_style PARAMS((HTStructured * me));
  82. PRIVATE void change_paragraph_style PARAMS((HTStructured * me, HTStyle * style));
  83.  
  84. /*    Style buffering avoids dummy paragraph begin/ends.
  85. */
  86. #define UPDATE_STYLE if (me->style_change) { actually_set_style(me); }
  87.  
  88.  
  89. #ifdef OLD_CODE
  90. /* The following accented characters are from peter Flynn, curia project */
  91.  
  92. /* these ifdefs don't solve the problem of a simple terminal emulator
  93. ** with a different character set to the client machine. But nothing does,
  94. ** except looking at the TERM setting */
  95.  
  96.  
  97.         { "ocus" , "&" },       /* for CURIA */
  98. #ifdef IBMPC
  99.         { "aacute" , "\240" },    /* For PC display */
  100.         { "eacute" , "\202" },
  101.         { "iacute" , "\241" },
  102.         { "oacute" , "\242" },
  103.         { "uacute" , "\243" },
  104.         { "Aacute" , "\101" },
  105.         { "Eacute" , "\220" },
  106.         { "Iacute" , "\111" },
  107.         { "Oacute" , "\117" },
  108.         { "Uacute" , "\125" },
  109. #else
  110.         { "aacute" , "\341" },    /* Works for openwindows -- Peter Flynn */
  111.         { "eacute" , "\351" },
  112.         { "iacute" , "\355" },
  113.         { "oacute" , "\363" },
  114.         { "uacute" , "\372" },
  115.         { "Aacute" , "\301" },
  116.         { "Eacute" , "\310" },
  117.         { "Iacute" , "\315" },
  118.         { "Oacute" , "\323" },
  119.         { "Uacute" , "\332" }, 
  120. #endif
  121.     { 0,    0 }  /* Terminate list */
  122. };
  123. #endif
  124.  
  125.  
  126. /*     Entity values -- for ISO Latin 1 local representation
  127. **
  128. **    This MUST match exactly the table referred to in the DTD!
  129. */
  130. static char * ISO_Latin1[] = {
  131.       "\306",    /* capital AE diphthong (ligature) */ 
  132.       "\301",    /* capital A, acute accent */ 
  133.       "\302",    /* capital A, circumflex accent */ 
  134.       "\300",    /* capital A, grave accent */ 
  135.       "\305",    /* capital A, ring */ 
  136.       "\303",    /* capital A, tilde */ 
  137.       "\304",    /* capital A, dieresis or umlaut mark */ 
  138.       "\307",    /* capital C, cedilla */ 
  139.       "\320",    /* capital Eth, Icelandic */ 
  140.       "\311",    /* capital E, acute accent */ 
  141.       "\312",    /* capital E, circumflex accent */ 
  142.       "\310",    /* capital E, grave accent */ 
  143.       "\313",    /* capital E, dieresis or umlaut mark */ 
  144.       "\315",    /* capital I, acute accent */ 
  145.       "\316",    /* capital I, circumflex accent */ 
  146.       "\314",    /* capital I, grave accent */ 
  147.       "\317",    /* capital I, dieresis or umlaut mark */ 
  148.       "\321",    /* capital N, tilde */ 
  149.       "\323",    /* capital O, acute accent */ 
  150.       "\324",    /* capital O, circumflex accent */ 
  151.       "\322",    /* capital O, grave accent */ 
  152.       "\330",    /* capital O, slash */ 
  153.       "\325",    /* capital O, tilde */ 
  154.       "\326",    /* capital O, dieresis or umlaut mark */ 
  155.       "\336",    /* capital THORN, Icelandic */ 
  156.       "\332",    /* capital U, acute accent */ 
  157.       "\333",    /* capital U, circumflex accent */ 
  158.     "\331",    /* capital U, grave accent */
  159.       "\334",    /* capital U, dieresis or umlaut mark */ 
  160.       "\335",    /* capital Y, acute accent */ 
  161.       "\341",    /* small a, acute accent */ 
  162.       "\342",    /* small a, circumflex accent */ 
  163.       "\346",    /* small ae diphthong (ligature) */ 
  164.       "\340",    /* small a, grave accent */ 
  165.       "\046",    /* ampersand */ 
  166.     "\345",    /* small a, ring */
  167.       "\343",    /* small a, tilde */ 
  168.       "\344",    /* small a, dieresis or umlaut mark */ 
  169.       "\347",    /* small c, cedilla */ 
  170.       "\351",    /* small e, acute accent */ 
  171.       "\352",    /* small e, circumflex accent */ 
  172.       "\350",    /* small e, grave accent */ 
  173.       "\360",    /* small eth, Icelandic */ 
  174.       "\353",    /* small e, dieresis or umlaut mark */ 
  175.       "\076",    /* greater than */ 
  176.       "\355",    /* small i, acute accent */ 
  177.       "\356",    /* small i, circumflex accent */ 
  178.       "\354",    /* small i, grave accent */ 
  179.       "\357",    /* small i, dieresis or umlaut mark */ 
  180.       "\074",    /* less than */ 
  181.       "\361",    /* small n, tilde */ 
  182.       "\363",    /* small o, acute accent */ 
  183.       "\364",    /* small o, circumflex accent */ 
  184.       "\362",    /* small o, grave accent */ 
  185.       "\370",    /* small o, slash */ 
  186.       "\365",    /* small o, tilde */ 
  187.       "\366",    /* small o, dieresis or umlaut mark */ 
  188.       "\337",    /* small sharp s, German (sz ligature) */ 
  189.       "\376",    /* small thorn, Icelandic */ 
  190.       "\372",    /* small u, acute accent */ 
  191.       "\373",    /* small u, circumflex accent */ 
  192.       "\371",    /* small u, grave accent */ 
  193.     "\374",    /* small u, dieresis or umlaut mark */
  194.       "\375",    /* small y, acute accent */ 
  195.       "\377",    /* small y, dieresis or umlaut mark */ 
  196. };
  197.  
  198.  
  199. /*     Entity values -- for NeXT local representation
  200. **
  201. **    This MUST match exactly the table referred to in the DTD!
  202. **
  203. */
  204. static char * NeXTCharacters[] = {
  205.       "\341",    /* capital AE diphthong (ligature)     */ 
  206.       "\202",    /* capital A, acute accent        */ 
  207.       "\203",    /* capital A, circumflex accent     */ 
  208.       "\201",    /* capital A, grave accent         */ 
  209.       "\206",    /* capital A, ring             */ 
  210.     "\204",    /* capital A, tilde             */
  211.       "\205",    /* capital A, dieresis or umlaut mark    */ 
  212.       "\207",    /* capital C, cedilla             */ 
  213.       "\220",    /* capital Eth, Icelandic         */ 
  214.       "\211",    /* capital E, acute accent                 */ 
  215.       "\212",    /* capital E, circumflex accent             */ 
  216.       "\210",    /* capital E, grave accent                 */ 
  217.       "\213",    /* capital E, dieresis or umlaut mark             */ 
  218.       "\215",    /* capital I, acute accent                 */ 
  219.       "\216",    /* capital I, circumflex accent     these are    */ 
  220.       "\214",    /* capital I, grave accent        ISO -100 hex    */ 
  221.       "\217",    /* capital I, dieresis or umlaut mark            */ 
  222.       "\221",    /* capital N, tilde                     */ 
  223.       "\223",    /* capital O, acute accent                 */ 
  224.       "\224",    /* capital O, circumflex accent             */ 
  225.       "\222",    /* capital O, grave accent                 */ 
  226.       "\351",    /* capital O, slash         'cept this */ 
  227.       "\225",    /* capital O, tilde                     */ 
  228.     "\226",    /* capital O, dieresis or umlaut mark            */
  229.       "\234",    /* capital THORN, Icelandic */ 
  230.       "\230",    /* capital U, acute accent */ 
  231.       "\231",    /* capital U, circumflex accent */ 
  232.       "\227",    /* capital U, grave accent */ 
  233.       "\232",    /* capital U, dieresis or umlaut mark */ 
  234.       "\233",    /* capital Y, acute accent */ 
  235.       "\326",    /* small a, acute accent */ 
  236.       "\327",    /* small a, circumflex accent */ 
  237.       "\361",    /* small ae diphthong (ligature) */ 
  238.       "\325",    /* small a, grave accent */ 
  239.       "\046",    /* ampersand */ 
  240.       "\332",    /* small a, ring */ 
  241.       "\330",    /* small a, tilde */ 
  242.       "\331",    /* small a, dieresis or umlaut mark */ 
  243.       "\333",    /* small c, cedilla */ 
  244.       "\335",    /* small e, acute accent */ 
  245.       "\336",    /* small e, circumflex accent */ 
  246.       "\334",    /* small e, grave accent */ 
  247.       "\346",    /* small eth, Icelandic     */ 
  248.       "\337",    /* small e, dieresis or umlaut mark */ 
  249.       "\076",    /* greater than */ 
  250.       "\342",    /* small i, acute accent */ 
  251.       "\344",    /* small i, circumflex accent */ 
  252.       "\340",    /* small i, grave accent */ 
  253.       "\345",    /* small i, dieresis or umlaut mark */ 
  254.     "\074",    /* less than */
  255.       "\347",    /* small n, tilde */ 
  256.       "\355",    /* small o, acute accent */ 
  257.       "\356",    /* small o, circumflex accent */ 
  258.       "\354",    /* small o, grave accent */ 
  259.       "\371",    /* small o, slash */ 
  260.       "\357",    /* small o, tilde */ 
  261.       "\360",    /* small o, dieresis or umlaut mark */ 
  262.       "\373",    /* small sharp s, German (sz ligature) */ 
  263.     "\374",    /* small thorn, Icelandic */
  264.       "\363",    /* small u, acute accent */ 
  265.       "\364",    /* small u, circumflex accent */ 
  266.       "\362",    /* small u, grave accent */ 
  267.       "\366",    /* small u, dieresis or umlaut mark */ 
  268.       "\367",    /* small y, acute accent */ 
  269.       "\375",    /* small y, dieresis or umlaut mark */ 
  270. };
  271.  
  272. /*     Entity values -- for IBM/PC Code Page 850 (International)
  273. **
  274. **    This MUST match exactly the table referred to in the DTD!
  275. **
  276. */
  277. /* @@@@@@@@@@@@@@@@@ TBD */
  278.  
  279.  
  280.  
  281. /*        Set character set
  282. **        ----------------
  283. */
  284.  
  285. PRIVATE char** p_entity_values = ISO_Latin1;    /* Pointer to translation */
  286.  
  287. PUBLIC void HTMLUseCharacterSet ARGS1(HTMLCharacterSet, i)
  288. {
  289.     p_entity_values = (i == HTML_NEXT_CHARS) ? NeXTCharacters
  290.                              : ISO_Latin1;
  291. }
  292.  
  293.  
  294. /*        Flattening the style structure
  295. **        ------------------------------
  296. **
  297. On the NeXT, and on any read-only browser, it is simpler for the text to have
  298. a sequence of styles, rather than a nested tree of styles. In this
  299. case we have to flatten the structure as it arrives from SGML tags into
  300. a sequence of styles.
  301. */
  302.  
  303. /*        If style really needs to be set, call this
  304. */
  305. PRIVATE void actually_set_style ARGS1(HTStructured *, me)
  306. {
  307.     if (!me->text) {            /* First time through */
  308.         me->text = HText_new2(me->node_anchor, me->target);
  309.         HText_beginAppend(me->text);
  310.         HText_setStyle(me->text, me->new_style);
  311.         me->in_word = NO;
  312.     } else {
  313.         HText_setStyle(me->text, me->new_style);
  314.     }
  315.     me->old_style = me->new_style;
  316.     me->style_change = NO;
  317. }
  318.  
  319. /*      If you THINK you need to change style, call this
  320. */
  321.  
  322. PRIVATE void change_paragraph_style ARGS2(HTStructured *, me, HTStyle *,style)
  323. {
  324.     if (me->new_style!=style) {
  325.         me->style_change = YES;
  326.     me->new_style = style;
  327.     }
  328.     me->in_word = NO;
  329. }
  330.  
  331. /*_________________________________________________________________________
  332. **
  333. **            A C T I O N     R O U T I N E S
  334. */
  335.  
  336. /*    Character handling
  337. **    ------------------
  338. */
  339. #ifdef MSDOS
  340. /*
  341.  *    To fix some screwed up HTML formatting.
  342.  */
  343. int i_CollapseSpaces = 1;
  344. #endif /* MSDOS */
  345.  
  346. PRIVATE void HTML_put_character ARGS2(HTStructured *, me, char, c)
  347. {
  348.  
  349.     switch (me->sp[0].tag_number) {
  350.     case HTML_COMMENT:
  351.     break;                    /* Do Nothing */
  352.  
  353.     case HTML_TITLE:
  354.     HTChunkPutc(&me->title, c);
  355.     break;
  356.  
  357.  
  358.     case HTML_LISTING:                /* Litteral text */
  359.     case HTML_XMP:
  360.     case HTML_PLAINTEXT:
  361.     case HTML_PRE:
  362. /*    We guarrantee that the style is up-to-date in begin_litteral
  363. */
  364. #ifdef MSDOS
  365.     i_CollapseSpaces = 0;
  366.     HText_appendCharacter(me->text, c);
  367.     i_CollapseSpaces = 1;
  368. #else
  369.     HText_appendCharacter(me->text, c);
  370. #endif /* MSDOS */
  371.     break;
  372.     
  373.     default:                    /* Free format text */
  374.     if (me->style_change) {
  375.         if ((c=='\n') || (c==' ')) return;    /* Ignore it */
  376.         UPDATE_STYLE;
  377.     }
  378.     if (c=='\n') {
  379.         if (me->in_word) {
  380.         HText_appendCharacter(me->text, ' ');
  381.         me->in_word = NO;
  382.         }
  383.     } else {
  384.         HText_appendCharacter(me->text, c);
  385.         me->in_word = YES;
  386.     }
  387.     } /* end switch */
  388. }
  389.  
  390.  
  391.  
  392. /*    String handling
  393. **    ---------------
  394. **
  395. **    This is written separately from put_character becuase the loop can
  396. **    in some cases be promoted to a higher function call level for speed.
  397. */
  398. PRIVATE void HTML_put_string ARGS2(HTStructured *, me, CONST char*, s)
  399. {
  400.  
  401.     switch (me->sp[0].tag_number) {
  402.     case HTML_COMMENT:
  403.         break;                    /* Do Nothing */
  404.     
  405.     case HTML_TITLE:    
  406.         HTChunkPuts(&me->title, s);
  407.     break;
  408.  
  409.     
  410.     case HTML_LISTING:                /* Litteral text */
  411.     case HTML_XMP:
  412.     case HTML_PLAINTEXT:
  413.     case HTML_PRE:
  414.  
  415. /*    We guarrantee that the style is up-to-date in begin_litteral
  416. */
  417. #ifdef MSDOS
  418.     i_CollapseSpaces = 0;
  419.     HText_appendText(me->text, s);
  420.     i_CollapseSpaces = 1;
  421. #else
  422.     HText_appendText(me->text, s);
  423. #endif /* MSDOS */
  424.     break;
  425.  
  426.     default:                    /* Free format text */
  427.     {
  428.         CONST char *p = s;
  429.         if (me->style_change) {
  430.         for (; *p && ((*p=='\n') || (*p==' ')); p++)  ;  /* Ignore leaders */
  431.         if (!*p) return;
  432.         UPDATE_STYLE;
  433.         }
  434.         for(; *p; p++) {
  435.         if (me->style_change) {
  436.             if ((*p=='\n') || (*p==' ')) continue;  /* Ignore it */
  437.             UPDATE_STYLE;
  438.         }
  439.         if (*p=='\n') {
  440.             if (me->in_word) {
  441.             HText_appendCharacter(me->text, ' ');
  442.             me->in_word = NO;
  443.             }
  444.         } else {
  445.             HText_appendCharacter(me->text, *p);
  446.             me->in_word = YES;
  447.         }
  448.         } /* for */
  449.     }
  450.     } /* end switch */
  451. }
  452.  
  453.  
  454. /*    Buffer write
  455. **    ------------
  456. */
  457. PRIVATE void HTML_write ARGS3(HTStructured *, me, CONST char*, s, int, l)
  458. {
  459.     CONST char* p;
  460.     CONST char* e = s+l;
  461.     for (p=s; s<e; p++) HTML_put_character(me, *p);
  462. }
  463.  
  464.  
  465. /*    Start Element
  466. **    -------------
  467. */
  468. PRIVATE void HTML_start_element ARGS4(
  469.     HTStructured *,     me,
  470.     int,        element_number,
  471.     CONST BOOL*,         present,
  472.     CONST char **,    value)
  473. {
  474. #ifdef MSDOSMEM
  475.     {
  476.         extern void *vp_msdosmem;
  477.         extern void **vpp_msdosmem;
  478.         present = (BOOL *)vp_msdosmem;
  479.         value = (char **)vpp_msdosmem;
  480.     }
  481. #endif /* MSDOSMEM */
  482.  
  483.     switch (element_number) {
  484.     case HTML_A:
  485.     {
  486.         HTChildAnchor * source;
  487.         char * href = NULL;
  488.         if (present[HTML_A_HREF]) {
  489.             StrAllocCopy(href, value[HTML_A_HREF]);
  490.         HTSimplify(href);
  491.         }
  492.         source = HTAnchor_findChildAndLink(
  493.         me->node_anchor,                /* parent */
  494.         present[HTML_A_NAME] ? value[HTML_A_NAME] : 0,    /* Tag */
  495.         present[HTML_A_HREF] ? href : 0,        /* Addresss */
  496.         present[HTML_A_TYPE] && value[HTML_A_TYPE] ?
  497.             (HTLinkType*)HTAtom_for(value[HTML_A_TYPE])
  498.                             : 0);
  499.         
  500.         if (present[HTML_A_TITLE] && value[HTML_A_TITLE]) {
  501.             HTParentAnchor * dest = 
  502.             HTAnchor_parent(
  503.             HTAnchor_followMainLink((HTAnchor*)source)
  504.                     );
  505.         if (!HTAnchor_title(dest))
  506.             HTAnchor_setTitle(dest, value[HTML_A_TITLE]);
  507.         }
  508.         UPDATE_STYLE;
  509.         HText_beginAnchor(me->text, source);
  510.     }
  511.         break;
  512.  
  513.     case HTML_TITLE:
  514.         HTChunkClear(&me->title);
  515.     break;
  516.  
  517.     case HTML_NEXTID:
  518.     /* if (present[NEXTID_N] && value[NEXTID_N])
  519.         HText_setNextId(me->text, atoi(value[NEXTID_N])); */
  520.         break;
  521.     
  522.     case HTML_ISINDEX:
  523.        HTAnchor_setIndex(me->node_anchor);
  524.     break;
  525.     
  526.     case HTML_BR: 
  527.     UPDATE_STYLE;
  528.     HText_appendCharacter(me->text, '\n');
  529.     me->in_word = NO;
  530.     break;
  531.  
  532.     case HTML_HR:
  533.     UPDATE_STYLE;
  534.     HText_appendCharacter(me->text, '\n');
  535. #ifdef MSDOS
  536. {
  537.     unsigned short int usi_count;
  538.     for(usi_count = 0; usi_count < 35; usi_count++)
  539.         HText_appendCharacter(me->text, '-');
  540. }
  541. #else
  542.     HText_appendCharacter(me->text, "___________________________________");
  543. #endif /* MSDOS */
  544.     HText_appendCharacter(me->text, '\n');
  545.     me->in_word = NO;
  546.     break;
  547.  
  548.     case HTML_P:
  549.     UPDATE_STYLE;
  550.     HText_appendParagraph(me->text);
  551.     me->in_word = NO;
  552.     break;
  553.  
  554.     case HTML_DL:
  555.     change_paragraph_style(me, present && present[DL_COMPACT]
  556.         ? styles[HTML_DLC]
  557.         : styles[HTML_DL]);
  558.     break;
  559.  
  560.     case HTML_DT:
  561.     if (!me->style_change) {
  562.         HText_appendParagraph(me->text);
  563.         me->in_word = NO;
  564.     }
  565.     break;
  566.  
  567.     case HTML_DD:
  568.     UPDATE_STYLE;
  569.     HTML_put_character(me, '\t');    /* Just tab out one stop */
  570.     me->in_word = NO;
  571.     break;
  572.  
  573.     case HTML_UL:
  574.     case HTML_OL:
  575.     case HTML_MENU:
  576.     case HTML_DIR:
  577.     change_paragraph_style(me, styles[element_number]);
  578.     break;
  579.  
  580.     case HTML_LI:
  581.     UPDATE_STYLE;
  582.     if (me->sp[0].tag_number != HTML_DIR)
  583.         HText_appendParagraph(me->text);
  584.     else
  585.         HText_appendCharacter(me->text, '\t');    /* Tab @@ nl for UL? */
  586.     me->in_word = NO;
  587.     break;
  588.  
  589.     case HTML_LISTING:                /* Litteral text */
  590.     case HTML_XMP:
  591.     case HTML_PLAINTEXT:
  592.     case HTML_PRE:
  593.     change_paragraph_style(me, styles[element_number]);
  594.     UPDATE_STYLE;
  595.     if (me->comment_end)
  596. #ifdef MSDOS
  597.     {
  598.         i_CollapseSpaces = 0;
  599.         HText_appendText(me->text, me->comment_end);
  600.         i_CollapseSpaces = 1;
  601.     }
  602. #else
  603.         HText_appendText(me->text, me->comment_end);
  604. #endif /* MSDOS */
  605.     break;
  606.  
  607.     case HTML_IMG:            /* Images -- ignore */
  608. #ifdef MSDOS
  609. #ifdef TODO
  610. #error Need a way to display a tag or anchor to an image.
  611. #endif /* TODO */
  612. #endif /* MSDOS */
  613.  
  614.     case HTML_HTML:            /* Ignore these altogether */
  615.     case HTML_HEAD:
  616.     case HTML_BODY:
  617.  
  618.  
  619.     case HTML_TT:            /* Physical character highlighting */
  620.     case HTML_B:            /* Currently ignored */
  621.     case HTML_I:
  622.     case HTML_U:
  623.     
  624.     case HTML_EM:            /* Logical character highlighting */
  625.     case HTML_STRONG:            /* Currently ignored */
  626.     case HTML_CODE:
  627.     case HTML_SAMP:
  628.     case HTML_KBD:
  629.     case HTML_VAR:
  630.     case HTML_DFN:
  631.     case HTML_CITE:
  632.         break;
  633.     
  634.     case HTML_H1:            /* paragraph styles */
  635.     case HTML_H2:
  636.     case HTML_H3:
  637.     case HTML_H4:
  638.     case HTML_H5:
  639.     case HTML_H6:
  640.     case HTML_H7:
  641.     case HTML_ADDRESS:
  642.     case HTML_BLOCKQUOTE:
  643.     change_paragraph_style(me, styles[element_number]);    /* May be postponed */
  644.     break;
  645.  
  646.     } /* end switch */
  647.  
  648.     if (HTML_dtd.tags[element_number].contents!= SGML_EMPTY) {
  649.         if (me->sp == me->stack) {
  650.         fprintf(stderr, "HTML: ****** Maximum nesting of %d exceded!\n",
  651.         MAX_NESTING); 
  652.         return;
  653.     }
  654.         --(me->sp);
  655.     me->sp[0].style = me->new_style;    /* Stack new style */
  656.     me->sp[0].tag_number = element_number;
  657.     }
  658. }
  659.  
  660.  
  661. /*        End Element
  662. **        -----------
  663. **
  664. */
  665. /*    When we end an element, the style must be returned to that
  666. **    in effect before that element.  Note that anchors (etc?)
  667. **    don't have an associated style, so that we must scan down the
  668. **    stack for an element with a defined style. (In fact, the styles
  669. **    should be linked to the whole stack not just the top one.)
  670. **    TBL 921119
  671. **
  672. **    We don't turn on "CAREFUL" check because the parser produces
  673. **    (internal code errors apart) good nesting. The parser checks
  674. **    incoming code errors, not this module.
  675. */
  676. PRIVATE void HTML_end_element ARGS2(HTStructured *, me, int , element_number)
  677. {
  678. #ifdef CAREFUL            /* parser assumed to produce good nesting */
  679.     if (element_number != me->sp[0].tag_number) {
  680.         fprintf(stderr, "HTMLText: end of element %s when expecting end of %s\n",
  681.         HTML_dtd.tags[element_number].name,
  682.         HTML_dtd.tags[me->sp->tag_number].name);
  683.         /* panic */
  684.     }
  685. #endif
  686.     
  687.     me->sp++;                /* Pop state off stack */
  688.     
  689.     switch(element_number) {
  690.  
  691.     case HTML_A:
  692.     UPDATE_STYLE;
  693.     HText_endAnchor(me->text);
  694.     break;
  695.  
  696.     case HTML_TITLE:
  697.         HTChunkTerminate(&me->title);
  698.         HTAnchor_setTitle(me->node_anchor, me->title.data);
  699.     break;
  700.     
  701.     case HTML_LISTING:                /* Litteral text */
  702.     case HTML_XMP:
  703.     case HTML_PLAINTEXT:
  704.     case HTML_PRE:
  705.     if (me->comment_start)
  706. #ifdef MSDOS
  707.     {
  708.         i_CollapseSpaces = 0;
  709.         HText_appendText(me->text, me->comment_start);
  710.         i_CollapseSpaces = 1;
  711.     }
  712. #else /* MSDOS */
  713.         HText_appendText(me->text, me->comment_start);
  714. #endif /* MSDOS */
  715.     /* Fall through */
  716.  
  717.     default:
  718.  
  719.     change_paragraph_style(me, me->sp->style);    /* Often won't really change */
  720.     break;
  721.     
  722.     } /* switch */
  723. }
  724.  
  725.  
  726. /*        Expanding entities
  727. **        ------------------
  728. */
  729. /*    (In fact, they all shrink!)
  730. */
  731.  
  732. PRIVATE void HTML_put_entity ARGS2(HTStructured *, me, int, entity_number)
  733. {
  734.     HTML_put_string(me, ISO_Latin1[entity_number]);    /* @@ Other representations */
  735. }
  736.  
  737.  
  738. /*    Free an HTML object
  739. **    -------------------
  740. **
  741. ** If the document is empty, the text object will not yet exist.
  742.    So we could in fact abandon creating the document and return
  743.    an error code.  In fact an empty document is an important type
  744.    of document, so we don't.
  745. **
  746. **    If non-interactive, everything is freed off.   No: crashes -listrefs
  747. **    Otherwise, the interactive object is left.    
  748. */
  749. PUBLIC void HTML_free ARGS1(HTStructured *, me)
  750. {
  751.     UPDATE_STYLE;        /* Creates empty document here! */
  752.     if (me->comment_end)
  753.         HTML_put_string(me,me->comment_end);
  754.     HText_endAppend(me->text);
  755.  
  756.     if (me->target) {
  757.         (*me->targetClass.free)(me->target);
  758.     }
  759.     free(me);
  760. }
  761.  
  762.  
  763. PRIVATE void HTML_abort ARGS2(HTStructured *, me, HTError, e)
  764.  
  765. {
  766.     if (me->target) {
  767.         (*me->targetClass.abort)(me->target, e);
  768.     }
  769.     free(me);
  770.  
  771. }
  772.  
  773.  
  774. /*    Get Styles from style sheet
  775. **    ---------------------------
  776. */
  777. PRIVATE void get_styles NOARGS
  778. {
  779.     got_styles = YES;
  780.  
  781.     default_style =        HTStyleNamed(styleSheet, "Normal");
  782.  
  783.     styles[HTML_H1] =        HTStyleNamed(styleSheet, "Heading1");
  784.     styles[HTML_H2] =        HTStyleNamed(styleSheet, "Heading2");
  785.     styles[HTML_H3] =        HTStyleNamed(styleSheet, "Heading3");
  786.     styles[HTML_H4] =        HTStyleNamed(styleSheet, "Heading4");
  787.     styles[HTML_H5] =        HTStyleNamed(styleSheet, "Heading5");
  788.     styles[HTML_H6] =        HTStyleNamed(styleSheet, "Heading6");
  789.     styles[HTML_H7] =        HTStyleNamed(styleSheet, "Heading7");
  790.  
  791.     styles[HTML_DL] =        HTStyleNamed(styleSheet, "Glossary");
  792.     styles[HTML_UL] =
  793.     styles[HTML_OL] =        HTStyleNamed(styleSheet, "List");
  794.     styles[HTML_MENU] =        HTStyleNamed(styleSheet, "Menu");
  795.     styles[HTML_DIR] =        HTStyleNamed(styleSheet, "Dir");    
  796.     styles[HTML_DLC] =        HTStyleNamed(styleSheet, "GlossaryCompact");
  797.     styles[HTML_ADDRESS]=    HTStyleNamed(styleSheet, "Address");
  798.     styles[HTML_BLOCKQUOTE]=    HTStyleNamed(styleSheet, "BlockQuote");
  799.     styles[HTML_PLAINTEXT] =
  800.     styles[HTML_XMP] =        HTStyleNamed(styleSheet, "Example");
  801.     styles[HTML_PRE] =        HTStyleNamed(styleSheet, "Preformatted");
  802.     styles[HTML_LISTING] =    HTStyleNamed(styleSheet, "Listing");
  803. }
  804. /*                P U B L I C
  805. */
  806.  
  807. /*    Structured Object Class
  808. **    -----------------------
  809. */
  810. PUBLIC CONST HTStructuredClass HTMLPresentation = /* As opposed to print etc */
  811. {        
  812.     "text/html",
  813.     HTML_free,
  814.     HTML_abort,
  815.     HTML_put_character,     HTML_put_string,  HTML_write,
  816.     HTML_start_element,     HTML_end_element,
  817.     HTML_put_entity
  818. }; 
  819.  
  820.  
  821. /*        New Structured Text object
  822. **        --------------------------
  823. **
  824. **    The strutcured stream can generate either presentation,
  825. **    or plain text, or HTML.
  826. */
  827. PUBLIC HTStructured* HTML_new ARGS3(
  828.     HTParentAnchor *,     anchor,
  829.     HTFormat,        format_out,
  830.     HTStream*,        stream)
  831. {
  832.  
  833.     HTStructured * me;
  834.     
  835.     if (format_out != WWW_PLAINTEXT && format_out != WWW_PRESENT) {
  836.         HTStream * intermediate = HTStreamStack(WWW_HTML, format_out,
  837.         stream, anchor);
  838.     if (intermediate) return HTMLGenerator(intermediate);
  839.         fprintf(stderr, "** Internal error: can't parse HTML to %s\n",
  840.         HTAtom_name(format_out));
  841. #ifndef MSDOS
  842.     exit (-99);
  843. #else
  844.     fprintf(stderr, "WWW html.c exit(-99); denied by doslynx.\n");
  845.     fprintf(stderr, "Consider leaving the application.\n");
  846. #endif /* MSDOS */
  847.     }
  848.  
  849.     me = (HTStructured*) malloc(sizeof(*me));
  850.     if (me == NULL) outofmem(__FILE__, "HTML_new");
  851.  
  852.     if (!got_styles) get_styles();
  853.  
  854.     me->isa = &HTMLPresentation;
  855.     me->node_anchor =  anchor;
  856.     me->title.size = 0;
  857.     me->title.growby = 128;
  858.     me->title.allocated = 0;
  859.     me->title.data = 0;
  860.     me->text = 0;
  861.     me->style_change = YES; /* Force check leading to text creation */
  862.     me->new_style = default_style;
  863.     me->old_style = 0;
  864.     me->sp = me->stack + MAX_NESTING - 1;
  865.     me->sp->tag_number = -1;                /* INVALID */
  866.     me->sp->style = default_style;            /* INVALID */
  867.     
  868.     me->comment_start = NULL;
  869.     me->comment_end = NULL;
  870.     me->target = stream;
  871.     if (stream) me->targetClass = *stream->isa;    /* Copy pointers */
  872.     
  873.     return (HTStructured*) me;
  874. }
  875.  
  876.  
  877. /*    HTConverter for HTML to plain text
  878. **    ----------------------------------
  879. **
  880. **    This will convert from HTML to presentation or plain text.
  881. */
  882. PUBLIC HTStream* HTMLToPlain ARGS3(
  883.     HTPresentation *,    pres,
  884.     HTParentAnchor *,    anchor,    
  885.     HTStream *,        sink)
  886. {
  887.     return SGML_new(&HTML_dtd, HTML_new(anchor, pres->rep_out, sink));
  888. }
  889.  
  890.  
  891. /*    HTConverter for HTML to C code
  892. **    ------------------------------
  893. **
  894. **    C copde is like plain text but all non-preformatted code
  895. **    is commented out.
  896. **    This will convert from HTML to presentation or plain text.
  897. */
  898. PUBLIC HTStream* HTMLToC ARGS3(
  899.     HTPresentation *,    pres,
  900.     HTParentAnchor *,    anchor,    
  901.     HTStream *,        sink)
  902. {
  903.     
  904.     HTStructured * html;
  905.     
  906.     (*sink->isa->put_string)(sink, "/* ");    /* Before even title */
  907.     html = HTML_new(anchor, WWW_PLAINTEXT, sink);
  908.     html->comment_start = "/* ";
  909.     html->comment_end = " */\n";    /* Must start in col 1 for cpp */
  910. /*    HTML_put_string(html,html->comment_start); */
  911.     return SGML_new(&HTML_dtd, html);
  912. }
  913.  
  914.  
  915. /*    Presenter for HTML
  916. **    ------------------
  917. **
  918. **    This will convert from HTML to presentation or plain text.
  919. **
  920. **    Override this if you have a windows version
  921. */
  922. #ifndef GUI
  923. PUBLIC HTStream* HTMLPresent ARGS3(
  924.     HTPresentation *,    pres,
  925.     HTParentAnchor *,    anchor,    
  926.     HTStream *,        sink)
  927. {
  928.     return SGML_new(&HTML_dtd, HTML_new(anchor, WWW_PRESENT, sink));
  929. }
  930. #endif
  931.  
  932.  
  933. /*    Record error message as a hypertext object
  934. **    ------------------------------------------
  935. **
  936. **    The error message should be marked as an error so that
  937. **    it can be reloaded later.
  938. **    This implementation just throws up an error message
  939. **    and leaves the document unloaded.
  940. **    A smarter implementation would load an error document,
  941. **    marking at such so that it is retried on reload.
  942. **
  943. ** On entry,
  944. **    sink     is a stream to the output device if any
  945. **    number    is the HTTP error number
  946. **    message    is the human readable message.
  947. **
  948. ** On exit,
  949. **    returns    a negative number to indicate lack of success in the load.
  950. */
  951.  
  952. PUBLIC int HTLoadError ARGS3(
  953.     HTStream *,     sink,
  954.     int,        number,
  955.     CONST char *,    message)
  956. {
  957.     HTAlert(message);        /* @@@@@@@@@@@@@@@@@@@ */
  958.     return -number;
  959.  
  960.